﻿using KinonekoSoftware.CodariaDB.Documents.Shenanigans.Forum;
using KinonekoSoftware.Studio.Models.Shenanigans.Forums;
using PlainTextMSG = KinonekoSoftware.Studio.Models.Shenanigans.Forums.PlainTextMSG;
using ImageMSG = KinonekoSoftware.Studio.Models.Shenanigans.Forums.ImageMSG;

namespace KinonekoSoftware.Studio.Pages.Shenanigans.Previewer
{
    public static class ForumMSGBuilder
    {
        private static readonly ForumMSG NoReferenceMSG;

        static ForumMSGBuilder()
        {
            NoReferenceMSG = new PlainTextMSG
            {
                Id     = ID.Get(),
                Target = null,
                Poster = null,
            };
        }

        public static ForumMSG Build(
            CharacterDataSource dataSource, 
            Dictionary<string, ForumMSG> map,
            ForumMSGCreationFlag flag, 
            string poster, 
            ForumObject reference = null)
        {
            ForumObject forumObj = flag switch
            {
                ForumMSGCreationFlag.Attachment => new AttachmentForumObject
                {
                    Id        = ID.Get(),
                    PosterID  = poster,
                    Removable = true,
                },

                ForumMSGCreationFlag.Image => new ImageForumObject
                {
                    Id        = ID.Get(),
                    PosterID  = poster,
                    Images    = new List<string>(10),
                    Removable = true,

                },
                ForumMSGCreationFlag.Reply => new ReplyForumObject
                {
                    Id          = ID.Get(),
                    PosterID    = poster,
                    ReferenceID = reference?.Id,
                    Removable   = true,

                },
                _ => new PlainTextForumObject
                {
                    Id        = ID.Get(),
                    PosterID  = poster,
                    Removable = true,
                },
            };

            return Build(dataSource, map, forumObj);
        }


        public static ForumMSG Build(
            CharacterDataSource dataSource,
            Dictionary<string, ForumMSG> map,
            ForumObject source)
        {
            var msg      = (ForumMSG)null;
            var posterID = source.PosterID;

            if (source is ReplyForumObject rfo)
            {
                //
                // 创建默认reply对象
                var reply = map.GetValueOrDefault(rfo.ReferenceID, NoReferenceMSG);

                //
                // 创建消息
                msg = new ReplyMSG
                {
                    Id      = rfo.Id,
                    Reply   = reply,
                    Target  = rfo,
                    Poster  = dataSource.ScopedGet(posterID),
                    Content = rfo.Content,
                };
            }

            if (source is PlainTextForumObject ptfo)
            {

                msg = new PlainTextMSG
                {
                    Id      = ptfo.Id,
                    Target  = ptfo,
                    Poster  = dataSource.ScopedGet(posterID),
                    Content = ptfo.Content,
                };
            }

            if (source is ImageForumObject ifo)
            {

                msg = new ImageMSG
                {
                    Id      = ifo.Id,
                    Target  = ifo,
                    Poster  = dataSource.ScopedGet(posterID),
                    Content = ifo.Content,
                    Images  = new ObservableCollection<string>(),
                };
            }

            if (source is AttachmentForumObject afo)
            {
                msg = new AttachmentMSG
                {
                    Id      = afo.Id,
                    Target  = afo,
                    Poster  = dataSource.ScopedGet(posterID),
                    Content = afo.Content,
                };
            }

            //
            //
            if (msg is not null)
            {
                msg.DislikeAmount = source.DislikeAmount;
                msg.LikeAmount    = source.LikeAmount;
            }

            //
            //
            return msg;
        }
    }
}